package com.dfbz;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.dfbz.entity.User;
import com.dfbz.mapper.UserMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

/**
 * @author lscl
 * @version 1.0
 * @intro:
 */
@SpringBootTest(classes = MyBatisPlusApplication.class)
@RunWith(SpringRunner.class)
public class Demo04_QueryWrapper {

    @Autowired
    private UserMapper userMapper;

    // 选择查询的字段
    @Test
    public void test1() throws Exception {
        QueryWrapper<User> wrapper = Wrappers.query();

        // 确定要查询的字段
        wrapper.select("id", "name", "sex");

        // in
        wrapper.in("id","1","2");

        // SQL: SELECT id,name,sex FROM user WHERE (id IN (?,?))
        List<User> userList = userMapper.selectList(wrapper);

        userList.forEach(System.out::println);
    }


    @Test
    public void test2() throws Exception {

        User user = new User();
        user.setId(1L);

        // user当做查询的条件
        QueryWrapper<User> wrapper = Wrappers.query(user);

        // 指定查询的列
        wrapper.select("id", "name", "sex");

        // SQL: SELECT id,name,sex FROM user WHERE id=?
        List<User> userList = userMapper.selectList(wrapper);

        userList.forEach(System.out::println);
    }
}
